package cn.kogee.org.controller;

import cn.kogee.basic.dto.ShopDto;
import cn.kogee.basic.exception.BusinessException;
import cn.kogee.basic.util.AjaxResult;
import cn.kogee.basic.util.ExcelUtils;
import cn.kogee.basic.util.PageList;
import cn.kogee.org.domain.Shop;
import cn.kogee.org.domain.ShopAuditLog;
import cn.kogee.org.query.ShopQuery;
import cn.kogee.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口类",description = "店铺接口的CRUD操作")
public class ShopController {
    @Autowired
    private IShopService shopService;
    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/shop - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/shop/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.findOne(id);
    }

    /**
     * 分页接口
     * @param
     * @return
     */
    // http://localhost:8080/shop - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给departmentQuery
    @ApiOperation(value = "分页接口")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/shop/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){//添加
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult bathremove(@RequestBody List<Long> ids){
        try {
            shopService.bathremove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
    @PostMapping("/settlement")
    @ApiOperation(value = "注册接口")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙");
        }
    }

    /**
     * 审核通过接口
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过接口")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙审核失败");
        }
    }

    /**
     * 审核驳回接口
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回接口")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditReject(shopAuditLog);
             return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙审核失败");
        }
    }

    /**
     * 店铺激活接口
     * @param shopId
     */
    @GetMapping("/active/{shopId}")
    @ApiOperation(value = "店铺激活接口")
    public void active(@PathVariable("shopId") Long shopId) {
        shopService.active(shopId);
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    @ApiOperation(value = "查询所有")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"数据.xlsx"
            ,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入接口")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
            shopService.saves(shops);
    }
    @GetMapping("/echarts")
    @ApiOperation(value = "echarts报表")
    public List<ShopDto> echartsData(){
             List<ShopDto> shopDtos=shopService.echartsData();
             return shopDtos;
    }
}
